home *** CD-ROM | disk | FTP | other *** search
/ Network Supervisor's Toolkit / Network Supervisor's Toolkit.iso / tools / pprd100 / lpr.pl < prev    next >
Perl Script  |  1996-07-10  |  3KB  |  102 lines

  1. #!/usr/local/bin/perl
  2.  
  3. # edit this to the printer hostname
  4. $them = 'printer';
  5. # lpt1, 2 or 3
  6. $printer = 'lpt1';
  7. # change only for testing
  8. $port = 'printer';
  9. #$port = 1515;
  10.  
  11. $login = getlogin || (getpwuid($<))[0] || "?";
  12.  
  13. $file = "stdin";
  14. &errexit("Stdin is not a file\n") unless (-f STDIN && ($dsize = -s STDIN));
  15.  
  16. require 'sys/socket.ph';
  17.  
  18. $sockaddr = 'S n a4 x8';
  19. chop($hostname = `hostname`);
  20. $num = sprintf("%03d", $$ % 1000);
  21.  
  22. ($name, $aliases, $proto) = getprotobyname('tcp');
  23. ($name, $aliases, $port) = getservbyname($port, 'tcp')
  24.     unless $port =~ /^\d+$/;
  25. ($name, $aliases, $type, $len, $thisaddr) = gethostbyname($hostname);
  26. ($name, $aliases, $type, $len, $thataddr) = gethostbyname($them);
  27.  
  28. socket(S, &PF_INET, &SOCK_STREAM, $proto) || &errexit("socket: $!\n");
  29.  
  30. $srcport = 0;
  31. foreach $p (721..731)
  32. {
  33.     $this = pack($sockaddr, &AF_INET, $p, $thisaddr);
  34.     last if bind(S, $this) && ($srcport = $p);
  35. }
  36. # otherwise use any old port
  37. if ($srcport == 0)
  38. {
  39.     $this = pack($sockaddr, &AF_INET, 0, $thisaddr);
  40.     bind(S, $this) || &errexit("bind: $!\n");
  41. }
  42.  
  43. $that = pack($sockaddr, &AF_INET, $port, $thataddr);
  44. connect(S, $that) || &errexit("connect: $!\n");
  45.  
  46. select(S); $| = 1; select(stdout);
  47.  
  48. print(S "\002$printer\n");
  49. &errexit("Broken data connection\n") unless defined($r = read(S, $ans, 1));
  50. &errexit("$printer: not available\n") if $ans ne "\0";
  51.  
  52. $cfile = "cfA$num$hostname";
  53. $dfile = "dfA$num$hostname";
  54. &controlfile();
  55. &datafile();
  56.  
  57. exit 0;
  58.  
  59. sub controlfile
  60. {
  61.     $ctlstring = "H$hostname\nP$login\nJ$file\nC$hostname\nL$login\nN$file\nl$dfile\n";
  62.     $csize = length($ctlstring);
  63.     $ctlstring .= "\0";
  64.  
  65.     print(S "\002$csize $cfile\n");
  66.     &errexit("Broken data connection\n") unless defined($r = read(S, $ans, 1));
  67.     &errexit("Control file error\n") if $ans ne "\0";
  68.  
  69.     print(S $ctlstring);
  70.     &errexit("Broken data connection\n") unless defined($r = read(S, $ans, 1));
  71.     &errexit("Control data rejected\n") if $ans ne "\0";
  72. }
  73.  
  74. sub datafile
  75. {
  76.     print(S "\003$dsize $dfile\n");
  77.     &errexit("Broken data connection\n") unless defined($r = read(S, $ans, 1));
  78.     &errexit("Printer error\n") if $ans ne "\0";
  79.  
  80.     # we append the ending '\0' on the last buffer to increase the
  81.     # chance that it will be sent off in the same packet, reducing
  82.     # the number of packets flowing
  83.     while ($dsize > 0)
  84.     {
  85.         $nread = read(STDIN, $buffer, 1024);
  86.         if (($dsize -= $nread) <= 0)
  87.         {
  88.             $buffer .= "\0";
  89.             ++$nread;
  90.         }
  91.         &errexit("write: $!\n") unless syswrite(S, $buffer, $nread);
  92.     }
  93.     &errexit("Broken data connection\n") unless defined($r = read(S, $ans, 1));
  94.     &errexit("Data rejected\n") if $ans ne "\0";
  95. }
  96.  
  97. sub errexit
  98. {
  99.     print STDERR @_;
  100.     exit 2;
  101. }
  102.